// 文件下载
export const fileDownload = (res: any, filename?: string) => {
  let reader = new FileReader(); // 创建一个file对象
  reader.readAsDataURL(res); // 转换为base64，可以直接放入a标签的href
  reader.onload = function (e: any) {
    // 转换完成，创建一个a标签用于下载
    let a = document.createElement("a");
    a.download = filename || "temp.docx";
    a.href = e.target.result;
    // 在body中插入a元素
    document.body.insertAdjacentElement("afterend", a);
    a.click();
    a.remove();
  };
};

// 判断两个数组是否相同
export const arraysAreEqual = (arr1: any[], arr2: any[]) => {
  if (arr1.length !== arr2.length) {
    return false;
  }
  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) {
      return false;
    }
  }
  return true;
};

export const parseStr = (str: any) => {
  try {
    switch (str) {
      case "undefined":
        return undefined;
      case "null":
        return null;
      default:
        return JSON.parse(str);
    }
  } catch {
    return str;
  }
};
